package com.mindainfo.groundwave.server.member.service;

import com.mindainfo.groundwave.common.api.member.service.IStafferService;
import com.mindainfo.groundwave.common.api.member.vo.StafferVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.DataGrid;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.server.base.dao.JedisDao;
import com.mindainfo.groundwave.server.member.bean.Staffer;
import com.mindainfo.groundwave.server.member.bean.SysDept;
import com.mindainfo.groundwave.server.member.bean.SysMember;
import com.mindainfo.groundwave.server.member.dao.IDeptDao;
import com.mindainfo.groundwave.server.member.dao.IMemberDao;
import com.mindainfo.groundwave.server.member.dao.IStafferDao;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by MD-01 on 2017/5/27.
 */
@Service
public class StafferService implements IStafferService{

    @Resource
    private IStafferDao stafferDao;
    @Resource
    private IDeptDao deptDao;
    @Resource
    private JedisDao jedisDao;
    @Resource
    private IMemberDao memberDao;

    @Override
    public CommonResult<DataGrid<StafferVo>> queryList(int start, int num,  String key) {

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
//        SysMember member = (SysMember) session.getAttribute("member");
//        Integer deptId = member.getDeptId();
//        List<Integer> deptIdList = new ArrayList<>();
//        deptIdList.add(deptId);

        List<Integer> sysDeptIdList = (List<Integer>)session.getAttribute("myDeptIdList");

        List<Staffer> staffers = stafferDao.queryList(start, num, sysDeptIdList, key);
        int total = stafferDao.queryListCount(sysDeptIdList,key);
        List<StafferVo> vos = new ArrayList<>();
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");
        for(Staffer staffer : staffers){
            StafferVo vo = new StafferVo();
            vo.setCreateTime(format.format(staffer.getCreateTime()));
            vo.setDeptId(staffer.getDeptId());
            SysDept dept = deptDao.get(staffer.getDeptId());
            if(dept!=null){
                vo.setDeptName(dept.getName());
            }
            vo.setName(staffer.getName());
            vo.setPhoneNum(staffer.getPhoneNum());
            vo.setStafferId(staffer.getId());

            if (staffer.getMember()!=null && staffer.getMember()){
                Integer memberId = staffer.getMemberId();
                SysMember sysMember = memberDao.get(memberId);
                vo.setMemberId(sysMember.getId());
                vo.setUsername(sysMember.getUsername());
            }
            vos.add(vo);
        }
        DataGrid<StafferVo> dataGrid = new DataGrid<>();
        dataGrid.setTotal(total);
        dataGrid.setRows(vos);

        return ResultFactory.ok(dataGrid);
    }

    @Override
    public CommonResult<Void> add(Integer staffId,String name, String phoneNum, Integer deptId) {

        Staffer staffer;
        if(staffId!=null){
            staffer = stafferDao.get(staffId);
            if(staffer==null){
                return ResultFactory.error("错误的人员的编号");
            }
        }else{
            staffer = new Staffer();
        }
        staffer.setName(name);
        staffer.setPhoneNum(phoneNum);
        staffer.setCreateTime(new Date());
        staffer.setDeptId(deptId);
        staffer.setId(jedisDao.getId("stafferId"));

        stafferDao.save(staffer);

        return ResultFactory.ok();
    }

    /**
     * 获取所有可用的人员列表,按部门分组
     * @return
     */
    public CommonResult<Map<String,List<StafferVo>>> queryUsableAll(){

        List<Staffer> stafferList = stafferDao.queryUsableAll();
        List<StafferVo> vos = new ArrayList<>();
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");
        for(Staffer staffer : stafferList){
            StafferVo vo = new StafferVo();
            vo.setCreateTime(format.format(staffer.getCreateTime()));
            vo.setDeptId(staffer.getDeptId());
            SysDept dept = deptDao.get(staffer.getDeptId());
            if(dept!=null){
                vo.setDeptName(dept.getName());
            }
            vo.setName(staffer.getName());
            vo.setPhoneNum(staffer.getPhoneNum());
            vo.setStafferId(staffer.getId());
            vos.add(vo);
        }

        Map<String,List<StafferVo>> map = new HashMap<>();
        for (StafferVo vo : vos){
            List<StafferVo> stafferVos = map.get(String.valueOf(vo.getDeptId()));
            if(stafferVos == null){
                stafferVos = new ArrayList<>();
                map.put(String.valueOf(vo.getDeptId()),stafferVos);
            }
            stafferVos.add(vo);
        }
        return ResultFactory.ok(map);
    }

    /**
     * 根据部门编号获取部门下面的人员
     * @param deptId
     * @return
     */
    public CommonResult<List<StafferVo>> queryByDeptId(Integer deptId){

        List<Staffer> stafferList = stafferDao.queryByDeptId(deptId);
        List<StafferVo> vos = new ArrayList<>();
        FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        for(Staffer staffer : stafferList){
            StafferVo vo = new StafferVo();
            Integer memberId = staffer.getMemberId();
            if(memberId!=null){
                SysMember member = memberDao.get(memberId);
                vo.setUsername(member.getUsername());
                vo.setMemberId(memberId);
            }
            vo.setStafferId(staffer.getId());
            vo.setPhoneNum(staffer.getPhoneNum());
            vo.setName(staffer.getName());
            Integer deptId1 = staffer.getDeptId();
            SysDept dept = deptDao.get(deptId1);
            if(dept!=null){
                vo.setDeptName(dept.getName());
                vo.setDeptId(dept.getId());
            }
            vo.setCreateTime(fdf.format(staffer.getCreateTime()));
            vos.add(vo);
        }
        return ResultFactory.ok(vos);
    }
}
